Lead: The main knowledge points in this period are the learning of the microcontroller timer counter, interrupt and serial port. SCM is really difficult for beginners to understand, many students who have studied SCM or electronic enthusiasts, even when they graduated, they still have nothing. Based on this, the electronic enthusiast network will integrate the "single-chip learning knowledge point Raiders", divided into four series, to readers, so stay tuned! This series also has collection and reference value for electronic engineers in the industry. See related series MCU learning knowledge points Raiders (1) MCU learning knowledge points Raiders (2) The three main points of the series: 15: MCU bit operation instruction 16: MCU timer and counter 17: The way of the microcontroller timer / counter 18: Interrupt system of single chip microcomputer 19: MCU timer, interrupt test 20: Microcontroller timer/counter experiment 21: Introduction to the serial port of the MCU 15. Single-chip bit operation instruction In the routines of the previous water lamps, we have become accustomed to the "bit" one is the light and the light of a lamp, and the instructions we learn are all introduced by "bytes": byte movement, addition, Subtraction, logic operations, shifting, etc. Use bytes to deal with some math problems, such as: controlling the temperature of the refrigerator, the volume of the TV, etc. is very intuitive, can be directly expressed by the value. However, if you use it to control the opening and closing of some switches, the lights are off and off, it is not straightforward. Remember the routine of the water lamp in our last class? We know that after the value sent to the P1 port, we can't immediately know which light is on and off, but we need to know it in binary. There are many occasions in the industry that need to deal with this kind of switch output. The relay is connected, and it is somewhat troublesome to use byte processing. Therefore, a bit processing mechanism is deliberately introduced in the 8031 ​​MCU. Bit addressing area In the 8031, a portion of the RAM and a portion of the SFR are bit-addressable, meaning that each bit of the RAM has its own address and can be manipulated directly with this address. The 16 bytes of the internal RAM 20H-2FH is the bit addressing area of ​​the 8031. See Figure 1. It can be seen that every bit in each RAM in this area we can directly find them with the bit address, instead of using the byte address, and then using the logic instruction. Special function register for bit addressable Some SFRs in the 8031 ​​are capable of bit addressing. These SFRs are characterized by their byte address being divisible by 8, such as A accumulator, B register, PSW, IP (interrupt priority control register), IE (interrupt). Enable control register), SCON (serial port control register), TCON (timer/counter control register), P0-P3 (I/O port latch). We are not familiar with some of the above SFRs, and we will explain them in detail when we explain the relevant content. Bit manipulation instruction In the hardware structure of the MCS-51 microcontroller, there is a bit processor (also known as a Boolean processor), which has a set of instruction sets for bit variable processing. When performing bit processing, CY (which is the carry bit we talked about earlier) is called the "bit accumulator". There is its own bit RAM, which is the 16-byte unit of the 20H-2FH internal RAM that we just talked about, that is, 128 bit units, and its own bit I/O space (ie P0.0....P0.7 , P1.0.......P1.7, P2.0.......P2.7, P3.0.......P3.7). Of course, in physical entities, they are identical to the original RAM and port used for byte addressing, or both RAM and port can be used in two ways. Bit transfer instruction MOV C, BIT MOV BIT, C The function of this set of instructions is to implement data transfer between the bit accumulator (CY) and other bit addresses. Example: MOV P1.0, CY; send the state in CY to the P1.0 pin (if it is arithmetic operation, we can know by observation what CY is now). MOV P1.0, CY; sends the status of P1.0 to CY. Bit correction instruction Bit clear instruction CLR C ; make CY=0 CLR bit ; Makes the bit address of the instruction equal to 0. Example: CLR P1.0; even if P1.0 becomes 0 Position 1 command SETB C ; make CY=1 SETB bit ; Makes the specified bit address equal to 1. Example: SETB P1.0; make P.0 become 1 Bit negation instruction CPL C ; Make CY equal to the original opposite value, from 1 to 0, from 0 to 1. CPL bit ; Makes the value of the specified bit equal to the original opposite value, from 0 to 1, from 1 to 0. Example: CPL P1.0 Take the experiment we have done as an example. If the original light is on, the light is off after executing this command. Otherwise, the original light is off. After this command is executed, the light is on. Bit logic instruction Bit and instruction ANL C, bit ; CY is the same as the value of the specified bit address, and the result is sent back to CY. ANL C, /bit; first removes the value in the specified bit address, inverts it, and then CY, and returns the result to CY, but note that the value in the specified bit address itself does not change. Example: ANL C, /P1.0 Before executing this instruction, CY=1, P1.0 is equal to 1 (light off), then CY=0 after executing this instruction, and P1.0 is also equal to 1. It can be verified by the following procedure: ORG 0000H AJMP START ORG 30H START: MOV SP, #5FH MOV P1, #0FFH SETB C ANL C, /P1.0 MOV P1.1, C; the result of the completion is sent to P1.1, the result should be that the light on P1.1 is on, and the light on P1.0 is still off. Bit or instruction ORL C,bit ORL C, /bit Let's analyze this function yourself, and then edit the verification program against the above routine to see if you are right? Bit conditional branch instruction CY transfer instruction JC rel JNC rel The function of the first instruction is to transfer if CY is equal to 1, and if it is not equal to 1, the order is executed. So where do you move to? We can understand this: JC label, if it is equal to 1, go to the label to execute. This instruction has been mentioned in the last lesson and will not be repeated. The second instruction is the opposite of the first instruction, that is, if CY=0, it is transferred, and if it is not equal to 0, it is executed sequentially. Of course, we also understand: JNC label Judgment variable transfer instruction JB bit,rel JNB bit,rel The first instruction is to transfer if the value in the specified bit is 1, otherwise the order is executed. Again, we can understand this instruction like this: JB bit, label The second instruction, please first analyze it yourself. Below we give a routine description: ORG 0000H LJMP START ORG 30H START:MOV SP, #5FH MOV P1, #0FFH MOV P3, #0FFH L1: JNB P3.2, L2; P3.2 is connected with a button. When it is pressed, P3.2=0 JNB P3.3, L3; P3.3 is connected with a button, when it is pressed, P3.3=0 LJM P L1 L2: MOV P1, #00H LJMP L1 L3: MOV P1, #0FFH LJMP L1 END Write the above routine to the film to see what happens......... Press the button connected to P3.2, the light of P1 is fully illuminated, release or press again, the light does not go out, then press the button connected to P3.3, the light will be completely extinguished. What is this like? Isn't this the function of "starting" or "stopping" that is often used in industrial sites? How to do it? Initially, send 0FFH to port P3, so that all the leads of P3 are at high level, then execute L1. If P3.2 is high level (the keys are not pressed), the sequence executes JNB P3.3, L3. The statement, too, if P3.3 is high (the key is not pressed), the LJMP L1 statement is executed in order. In this way, P3.2 and P3.3 are continuously detected. If the button on P3.2 is pressed once, it will be transferred to L2, and MOV P1, #00H will be executed to make the light be fully lit, and then turn to L1. Cycle again until P3.3 is detected as 0, then go to L3, execute MOV P1, #0FFH, the lamp is completely off, then go to L1, so looping. Can you change the program with JB instructions with a little change? Shenzhen Isource lighting Co., Ltd , https://www.isourceled.com